package com.aerfafish.spider.neo4j.spider.service.impl;

import com.aerfafish.spider.neo4j.dao.FundEstablishDate;
import com.aerfafish.spider.neo4j.repository.FundEstablishDateRepository;
import com.aerfafish.spider.neo4j.spider.service.FundEstablishService;
import org.neo4j.driver.exceptions.TransientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.stereotype.Service;

@Service
public class FundEstablishServiceImpl implements FundEstablishService {

    @Autowired
    FundEstablishDateRepository fundEstablishDateRepository;

    @Override
    public void save(FundEstablishDate fundEstablishDate) throws Exception {
        save(fundEstablishDate.getFund().getCode(), fundEstablishDate.getDate().getDate());
    }

    @Override
    public void save(String fundCode, String date) throws Exception {
        if (date == null) {
            return;
        }

        if (fundCode == null) {
            throw new Exception("some param is null");
        }
        try {
            fundEstablishDateRepository.save(fundCode, date);
        } catch (TransientException | InvalidDataAccessApiUsageException e) {
            try {
                fundEstablishDateRepository.save(fundCode, date);
            } catch (TransientException | InvalidDataAccessApiUsageException e0) {
                fundEstablishDateRepository.save(fundCode, date);
            }
        }

//        if (fundEstablishDates.size() > 1) {
//            throw new Exception("fundCode: " + fundCode +" Date: " + date + " is repeated");
//        }
    }
}
